<script>
  function success(msg, fun) {
    $.cxDialog({
      title: '消息提示',
      info: msg,
      closeBtn: false
    });
    setTimeout(function () {
      $.cxDialog.close();
      fun()
    }, 2000);
  }

  function error(msg, fun) {
    $.cxDialog({
      title: '消息提示',
      info: msg,
      closeBtn: false
    });
    setTimeout(function () {
      $.cxDialog.close();
      fun()
    }, 2000);
  }

  function confirm(info, fun, nfun) {
    $.cxDialog({
      title: '消息提示',
      info: info,
      okText: '确认兑换',
      noText: '我再想想',
      ok: function () {
        fun()
      },
      no: function () {
        $.cxDialog.close();
      }
    });
  }
</script>
<script type="text/javascript">

  function exchange() {
    $('body').mLoading('show');
    var sourceAmount = $('#sourceAmount').val();
    if (sourceAmount <= 0) {
      error('请输入兑换积分', function () {
      })
      return false;
    }
    var info = '你正在兑换的金额为:' + sourceAmount + ',是否确认兑换?';
    confirm(info, function () {
      var sourcePointType = 10;
      if ($("#sourceType").val() == "static")
        sourcePointType = 10;
      else if ($("#sourceType").val() == "dynamic")
        sourcePointType = 11;
      var targetPointType = 3;
      if ($("#targetType").val() == "electronic")
        targetPointType = 3;

      $.post('/transaction/exchangePoint', {
          userCode: $('#userName').val(),
          sourcePointType: sourcePointType,
          sourcePointAmount: $('#sourceAmount').val(),
          targetPointType: targetPointType,
          targetPointAmount: $('#targetAmount').val(),
          tradePassword: $.md5($('#payPassword').val())
        },
        function (data) {
          if (data.retCode == '1') {
            $('body').mLoading('hide');
            success(data.retMsg, function () {
//              location.href = "/index.html#!/account/giro/exchange";
              location.reload();
            })
          } else {
            $('body').mLoading('hide');
            error(data.retMsg, function () {
            })
          }
        }
      )
    })
  };

  function changeSourceType(obj) {
    var style = $(obj).val();
    if (style == 'static') {
      $('.tipsTop').html('静态积分兑换电子积分收取20%积分作为复消积分，5%积分管理费，5%公益积分。');
      $('#errorMessage').html('提取积分-管理费-公益积分-复消积分');
    }
    if (style == 'dynamic') {
      $('.tipsTop').html('动态积分100的倍数，500元起提，兑换收取20%积分作为复消积分，5%积分管理费，5%公益积分。');
      $('#errorMessage').html('提取积分-管理费-公益积分-复消积分');
    }
    $('#sourceAmount').val('');
    $('#targetAmount').val('');
  };

  function changeTargetType(obj) {
    calculatePoint();
  };

  function calculatePoint() {

    $('#targetAmount').val('正在计算...');

    var sourcePointType = 10;
    if ($("#sourceType").val() == "static")
      sourcePointType = 10;
    else if ($("#sourceType").val() == "dynamic")
      sourcePointType = 11;

    var targetPointType = 3;
    if ($("#targetType").val() == "electronic")
      targetPointType = 3;

    $.post('/transaction/calculatePoint', {
      sourcePointType: sourcePointType,
      sourcePointAmount: $('#sourceAmount').val(),
      targetPointType: targetPointType,
    }, function (data) {
      $('#targetAmount').val(data.data.pointAmount);
    })
  };

  function getUser() {
    $.post('/user/currentUser', {}, function (data) {
      if (data.retCode == 1) {
        $('#userName').val(data.data.userName);
      }
    })
  };

  function getPoint() {
    $.post('/transaction/getPoint', {}, function (data) {
      if (data.retCode == 1) {
        $('option[value=static]').html("静态积分(" + data.data.cpAvailableStaticPoint + ")");
        $('option[value=dynamic]').html("动态积分(" + data.data.cpDynamicPoint + ")");
      } else {

      }
    })
  };

  $(function () {

    $('#btnExchange').bind('click', exchange);

    getUser();
    getPoint();

  });

</script>

<div id="memberMain" class="success">
  <div class="title">
    <h4>兑换管理</h4>
  </div>
  <div class="tipsTop">
    静态积分兑换电子积分收取20%积分作为复消积分，5%积分管理费，5%公益积分。
  </div>
  <div class="height18"></div>
  <form autocomplete="off" id="recharge-form">
    <ul class="formUl" id="userBank">
      <li class="">
        <label class="inputName">会员编号</label>
        <input class="inputText cardName" name="userName" value="" id="userName" type="text"
               style="color: rgb(0, 0, 0);" disabled="">
      </li>
      <li class="">
        <label class="inputName">积分类型</label>
        <select id="sourceType" name="sourceType" onchange="changeSourceType(this)">
          <option value="static" selected>静态积分(0.00)</option>
          <option value="dynamic">动态积分(0.00)</option>
        </select>
      </li>
      <li class="">
        <label class="inputName">兑换类型</label>
        <select id="targetType" name="targetType" onchange="changeTargetType(this)">
          <option value="electronic" selected>电子积分</option>
        </select>
      </li>
      <li class="">
        <label class="inputName">提取积分</label>
        <input id="sourceAmount" name="sourceAmount" class="inputText" maxlength="32" type="text"
               style="color: rgb(0, 0, 0);"
               onchange="calculatePoint(this)">
      </li>
      <li class="">
        <label class="inputName">兑换积分</label>
        <input class="inputText" maxlength="32" value="0.00" name="targetAmount" id="targetAmount" type="text"
               style="color: rgb(0, 0, 0);" disabled="">
        <div class="errorMessage" id="errorMessage">
          提取积分-管理费-公益积分-复消积分
        </div>
      </li>
      <li class="">
        <label class="inputName">交易密码</label>
        <input class="inputText" minlength="6" maxlength="16" name="payPassword" id="payPassword" type="password"
               style="color: rgb(0, 0, 0);">
      </li>
    </ul>
    <ul class="formUl">
      <li>
        <input class="inputBut showL inputBtnBig" type="button" value="确认" id="btnExchange">
      </li>
    </ul>
  </form>
</div>


